/*
* Nom de classe : MapDAO
*
* Description   : DAO pour map
*
* Version       : 0.0
*
* Date          : 31/01/2014
*
* Update     	: Julian
*/
package org.exia.minisig.model.dao;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.exia.minisig.application.LogFile;
import org.exia.minisig.application.Utils;
import org.exia.minisig.model.database.Database;
import org.exia.minisig.model.object.DescriptibleElement;
import org.exia.minisig.model.object.Place;

/**
* MapDAO
* @version0.0
* @see Itinerary
* @date 31/01/2013
*/
public class PlaceDAO  extends Database implements DAO {
	//VAR
	private Place place;
	//CONSTRUCTOR
	public PlaceDAO(){}
	public PlaceDAO(Place _place){
		initializeVar(_place);
	}
	public void initializeVar(Place _place){
		this.place = _place;
	}
	//OVERRIDE
	@Override
	public void add() {
		 try {
			  	this.connect();
		   		String jQuery = "INSERT INTO PLACES (LabelPlace, TextPlace)" +
		       "VALUES  ('" + this.place.getLabel() + "', '" + this.place.getText() + "')";
		   		Statement addStatement;
			   addStatement = connection.createStatement();
			   addStatement.executeUpdate(jQuery);
			   addStatement.close();
		  	} catch (SQLException e) {
			  	LogFile.setEntry("Erreur - ajout itineraire a la bdd\n" + e.getCause());
				System.out.println(e.getMessage());
		  	} finally {
			   this.close();
		    }	
	}
	@Override
	public void delete() {
		 try {
			  	this.connect();
			  	
		   		String jQuery = "DELETE FROM PLACES WHERE IdPlace = " + this.place.getId() + ";";
		   		Statement addStatement;
			   addStatement = connection.createStatement();
			   addStatement.executeUpdate(jQuery);
			   addStatement.close();
		  	} catch (SQLException e) {
			  	LogFile.setEntry("Erreur - ajout itineraire a la bdd\n" + e.getCause());
				System.out.println(e.getMessage());
		  	} finally {
			   this.close();
		    }


	}
	@Override
	public void update() {
		// TODO Auto-generated method stub

		try {
			this.connect();
			
			String jQuery= "UPDATE PLACES SET LabelPlace = '" + this.place.getLabel() + "', TextPlace = '" + this.place.getText() + "' WHERE IdPlace = "+ this.place.getId() + ";";
			Statement addStatement;
			   addStatement = connection.createStatement();
			   addStatement.executeUpdate(jQuery);
			   addStatement.close();
		  	} catch (SQLException e) {
			  	LogFile.setEntry("Erreur - update place\n" + e.getMessage());
				System.out.println(e.getMessage());
		  	} finally {
			   this.close();
		    }

	}
	@Override
	public ArrayList<Object> getList() {
		this.connect();
		ArrayList<Object> resultList = new ArrayList<>();
			try {
				Statement st = this.connection.createStatement();
				ResultSet rs= st.executeQuery("SELECT * FROM PLACES ");
				try {					
					while (rs.next())
					   { 
						Place currentPlace = new Place();
						currentPlace.setText(rs.getString("TextPlace"));
						currentPlace.setLabel(rs.getString("LabelPlace"));
						currentPlace.setId(Integer.parseInt(rs.getString("IdPlace")));
						resultList.add(currentPlace);
					    }						
				} catch (SQLException e) {
					System.out.println("Erreur creation liste des place en bdd");
				}
				for (Object object : resultList) {
					ResultSet rss= st.executeQuery("SELECT UrlPictures FROM PICTURES WHERE IdPlace = "+ ((DescriptibleElement) object).getId()+";");
					try{
						while (rss.next()){
							((DescriptibleElement) object).addImage(rss.getString("UrlPictures"));
						}
					}
					catch(SQLException e1){
						System.err.println("erreur ajout image lors de creation liste des place");
					}
				}		
				st.close();
				connection.close();
			} 
			catch (SQLException e2) {
				System.err.println("erreur getList Places");
			}			
			
			for (Object object : resultList) {
				Place obj = (Place)object;
				InterestDAO interest = new InterestDAO(obj.getId());
				obj.setInterests(interest.selectFromMap());
			}
		return resultList;
	}
	//METHODE
	@Override
	public void setID() {
		 try {
			  this.connect();
			  	//RECUPERATION ID
			   	String jQuery = " SELECT IdPlace FROM PLACES ORDER BY IdPlace DESC";
			   	Statement addStatement;
			   	addStatement = connection.createStatement();
		   		ResultSet result = addStatement.executeQuery(jQuery);
			   	while (result.next()) {
			   		this.place.setId(result.getInt("IdPlace"));
			    	break;
			   	}
			   	addStatement.close();
			   	result.close();
		  	} catch (SQLException e) {
			  	LogFile.setEntry("Erreur - set id place \n" + e.getCause());
				System.out.println(e.getMessage());
		  	} finally {
		  		this.close();
		    }
	}
	public void linkImages() {
		for (String img: this.place.getImages()) {
			//1COPY
				   File fileImg = new File(img);
				   String test = fileImg.getName();
				   String test2= Utils.applicationFolder() + "Place_img\\"+test;
				   fileImg.renameTo(new File(Utils.applicationFolder() + "Place_img\\" + fileImg.getName()));
			//
			   try{
				   this.connect();
				   //QUERY
				   String jQuery = "INSERT INTO PICTURES (UrlPictures, IdPlace)  VALUES('"+ test2 +"'," + this.place.getId() +");";
				   //EXECUTION
				   Statement addStatement;
				   addStatement = connection.createStatement();
				   addStatement.executeQuery(jQuery);
				   addStatement.close();
			   }catch(SQLException e){
			   }finally{
			    this.close();
			   }
		}
	}
	
	public void deleteImg(){
		try {
		  	this.connect();
	   		   		   		
	   		String jQuery = "DELETE FROM PICTURES WHERE IdPlace =("+this.place.getId()+");";
	   		Statement addStatement;
		   addStatement = connection.createStatement();
		   addStatement.executeUpdate(jQuery);
		   addStatement.close();
	  	} catch (SQLException e) {
		  	LogFile.setEntry("Erreur - ajout itineraire a la bdd\n" + e.getMessage());
			System.out.println(e.getMessage());
	  	} finally {
		   this.close();
	    }
	}

}
